package moonhy.c2dm_client.util;

import moonhy.c2dm_client.R;

import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.util.Log;

public class versionCodeManager {
	private SharedPreferences prefSet;
	
	public versionCodeManager(Context context){
		try {
			httpConnectionManager hcm = new httpConnectionManager(context);
			
			JSONObject json = new JSONObject(hcm.readJSON(context.getString(R.string.SERVER_URL) + "info/service_app_info.php", null));
			
			JSONObject jsonObject = json.getJSONObject("header");			
			
			prefSet = PreferenceManager.getDefaultSharedPreferences(context);
			Editor edit = prefSet.edit();
			edit.putInt("CUR_MEMBER_CODE", jsonObject.getInt("MEMBER_CODE"));
			edit.putInt("CUR_NOTICE_CODE", jsonObject.getInt("NOTICE_CODE"));
			edit.putInt("CUR_GROUP_CODE", jsonObject.getInt("GROUP_CODE"));
			edit.putInt("CUR_VERSION_CODE", jsonObject.getInt("APP_VERSION"));
			edit.commit();
			
			Log.d("C2DM", " CURRENT VERSION: " + jsonObject.getInt("APP_VERSION"));
			Log.d("C2DM", " CURRENT MEMBER_CODE: " + jsonObject.getInt("MEMBER_CODE"));
			Log.d("C2DM", " CURRENT NOTICE_CODE: " + jsonObject.getInt("NOTICE_CODE"));
			Log.d("C2DM", " CURRENT GROUP_CODE: " + jsonObject.getInt("GROUP_CODE"));
						
	    }catch(Exception e){
	    	Log.d("C2DM", "ERROR: info/service_app_info  -----> " + e.toString());
	    }
	}
	
	public int getCurrentVersionCode(){
		return prefSet.getInt("CUR_VERSION_CODE", 0);
	}
}
